package com.qdhh.enrollment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qdhh.enrollment.mapper.StudentProfileMapper;
import com.qdhh.enrollment.model.StudentProfile;
import com.qdhh.enrollment.service.StudentProfileService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 学生档案服务实现类
 * 根据文档设计，student_profiles表通过student_no（学号）作为主关联键
 */
@Service
public class StudentProfileServiceImpl extends ServiceImpl<StudentProfileMapper, StudentProfile>
    implements StudentProfileService {

    @Override
    public StudentProfile getByStudentNo(String studentNo) {
        if (!StringUtils.hasText(studentNo)) {
            return null;
        }
        return getOne(new LambdaQueryWrapper<StudentProfile>()
            .eq(StudentProfile::getStudentNo, studentNo)
            .eq(StudentProfile::getDeleted, 0), false);
    }

    @Override
    public StudentProfile getByPhone(String phone) {
        if (!StringUtils.hasText(phone)) {
            return null;
        }
        return getOne(new LambdaQueryWrapper<StudentProfile>()
            .eq(StudentProfile::getPhone, phone)
            .eq(StudentProfile::getDeleted, 0), false);
    }

    @Override
    public StudentProfile upsertByStudentNo(String studentNo, StudentProfile patch) {
        if (!StringUtils.hasText(studentNo)) {
            return null;
        }
        StudentProfile existing = getByStudentNo(studentNo);
        LocalDateTime now = LocalDateTime.now();
        
        if (existing == null) {
            // 创建新记录
            StudentProfile toSave = StudentProfile.builder()
                .studentNo(studentNo)
                .name(patch.getName())
                .gender(patch.getGender())
                .college(patch.getCollege())
                .major(patch.getMajor())
                .className(patch.getClassName())
                .phone(patch.getPhone())
                .email(patch.getEmail())
                .syncFrom(patch.getSyncFrom() != null ? patch.getSyncFrom() : "手动录入")
                .syncAt(now)
                .deleted(0)
                .version(1)
                .createdAt(now)
                .updatedAt(now)
                .build();
            save(toSave);
            return toSave;
        }

        // 更新现有记录
        boolean needUpdate = false;
        if (StringUtils.hasText(patch.getName())) {
            existing.setName(patch.getName());
            needUpdate = true;
        }
        if (StringUtils.hasText(patch.getGender())) {
            existing.setGender(patch.getGender());
            needUpdate = true;
        }
        if (StringUtils.hasText(patch.getCollege())) {
            existing.setCollege(patch.getCollege());
            needUpdate = true;
        }
        if (StringUtils.hasText(patch.getMajor())) {
            existing.setMajor(patch.getMajor());
            needUpdate = true;
        }
        if (StringUtils.hasText(patch.getClassName())) {
            existing.setClassName(patch.getClassName());
            needUpdate = true;
        }
        if (StringUtils.hasText(patch.getPhone())) {
            existing.setPhone(patch.getPhone());
            needUpdate = true;
        }
        if (StringUtils.hasText(patch.getEmail())) {
            existing.setEmail(patch.getEmail());
            needUpdate = true;
        }
        if (StringUtils.hasText(patch.getSyncFrom())) {
            existing.setSyncFrom(patch.getSyncFrom());
            needUpdate = true;
        }
        
        if (needUpdate) {
            existing.setSyncAt(now);
            existing.setUpdatedAt(now);
            updateById(existing);
        }
        return existing;
    }
}
